package xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * @Author wangzhipeng
 * @Date 2020-08-25 8:01 下午
 * @Description
 */
public class MySAXParser {
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        //创建解析工厂
        SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
        //创建解析器
        SAXParser saxParser = saxParserFactory.newSAXParser();
        //通过解析器的parser方法
        saxParser.parse("src/main/java/xml/person.xml",new MyDefaultHandler());

    }
}

class MyDefaultHandler extends DefaultHandler{
    public MyDefaultHandler() {
        super();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        System.out.print("<"+qName+">");
    }
    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        super.characters(ch, start, length);
        System.out.print(new String(ch,start,length));
    }
    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        System.out.print("</"+qName+">");
    }


}
